<template>
  <div class="group">
    <div class="page-head">
      <div class="page-title">设备分组</div>
      <div class="page-tool">
        <div
          class="tool-btn"
          @click="open('add')"
        >
          新增分组
        </div>
      </div>
    </div>
    <div class="page-body">
      <div class="table_data">
        <el-table :data="tableData" stripe border :fit="true" :height="$tableHeight">
          <el-table-column
            prop="name"
            label="分组名称"
          >
          </el-table-column>
          <el-table-column
            prop="total"
            label="设备数量"
          >
          </el-table-column>
          <el-table-column
            :show-overflow-tooltip="true"
            prop="remark"
            label="备注"
          >
          </el-table-column>

          <el-table-column label="操作" align="center" width="160">
            <template slot-scope="{ row }">
              <div class="operation-col">
                <div
                  class="txt-btn ope-btn"
                  @click="openGroup(row)"
                >
                  <el-tooltip effect="dark" content="组内设备管理" placement="top-start">
                    <img src="~assets/img/group.png" alt="">
                  </el-tooltip>
                </div>
                <div
                  class="txt-btn ope-btn"
                  @click="open('update', row)"
                >
                  <el-tooltip effect="dark" content="修改" placement="top-start">
                    <img src="~assets/img/edit.png" alt="">
                  </el-tooltip>
                </div>
                <div
                  class="txt-btn"
                  @click="doDelete(row)"
                >
                  <el-tooltip effect="dark" content="删除" placement="top-start">
                    <img src="~assets/img/delete.png" alt="">
                  </el-tooltip>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="page-footer">
        <pagina-tion
          class="page-pager"
          v-show="total >= 0"
          :background="true"
          :total="total"
          :offset.sync="offset"
          :limit.sync="limit"
          @pagination="getChangePage"
          @sizeChange="getSizePage"
        />
      </div>
    </div>

    <!-- 新增和修改弹框 -->
    <el-dialog
      :visible.sync="model.visible"
      width="370px"
      :title="model.type === 'add' ? '新增分组' : '修改分组'"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="dialog-body">
        <el-form
          ref="form"
          :model="form"
          label-width="110px"
          label-position="left"
        >
          <el-form-item label="分组名称" class="required">
            <el-input
              v-model="form.name"
              name="groupName"
              v-validate="'required'"
            />
            <div v-show="errors.has('groupName')" class="el-form-item__error">
              {{ errors.first("groupName") }}
            </div>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" type="textarea" maxlength="128" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 组内设备管理 -->
    <el-dialog
      :visible.sync="groupVisable"
      width="780px"
      title="组内设备管理"
      :close-on-click-modal="false"
      :show-close="false"
    >
      <div class="dialog-body">
        <el-transfer
          v-model="value"
          :data="list"
          :props="{
            key: 'hub_id',
            label: 'alias'
          }"
          filterable
          :filter-method="filterMd"
          filter-placeholder="设备搜索"
          :titles="['可选设备','已选设备']"
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="closeGroup">取 消</el-button>
        <el-button type="primary" @click="serialUpdate">确 定</el-button>
      </span>
    </el-dialog>
    
    <!-- 删除弹窗 -->
    <el-dialog
      :visible.sync="visibility"
      width="430px"
      height="100px"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="dialog-content comfirm-body">
        <img class="comfirm-ico" src="~assets/img/warning.png" />
        <div class="comfirm-txt">
          <div>确定删除该条数据？</div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="visibility = false">取 消</el-button>
        <el-button type="primary" @click="asyncDelete">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api";
import { limit, smallLimit } from "@/consts/consts";
import { groupKey } from "@/consts/formKey";
import paginaTion from '@/components/libs/paginaTion'


export default {
  components:{
    paginaTion,
  },

  data() {
    return {
      limit,
      total: 0,
      offset: 0,
      tableData: [],
      model: {
        type: "add",
        visible: false,
      },
      id: '',
      form: {
        ...groupKey
      },
      visibility: false,

      groupVisable: false,

      list: [],
      value: [],
      
      canCheckList: [],
      checkedList: [],
      
      c1: ''
    }
  },


  methods: {
    filterMd(query, item) {
      return item.alias.indexOf(query) > -1
    },
    open(type, row = null) {
      this.$validator.reset();
      this.model.type = type;
      if (row) {
        for (const k in groupKey) {
          this.form[k] = row[k];
        }
        this.id = row.group_id;
      }

      this.model.visible = true;
    },

    openGroup(row) {
      this.id = row.group_id
      this.serialList()
      
    },

    closeGroup() {
      this.form = { ...groupKey };
      this.groupVisable = false;
    },

    getChangePage(val) {
      this.offset = val.offset;
      this.getList();
    },
    getSizePage(val) {
      this.offset = 0
      this.getList();
    },
    doDelete(row) {
      this.id = row.group_id
      this.visibility = true
    },
    close() {
      this.model.visible = false;
      this.form = { ...groupKey };
    },

    submit() {
      this.$validator.validateAll().then((valid) => {
        if (valid) {
          this.addOrEdit();
        } else {
          this.$message.warning("有信息未填写正确");
        }
      });
    },


    
    // 5.2.5.组内已选设备
    async serialList() {
      const { code, data } = await api.group(this).serialList({
        group_id: this.id,
      });
      if (code === 0) {
        this.list = data
        let values = []
        data.forEach(d => {
          if (d.checked == '1') {
            values.push(d.hub_id)
          } 
        })
        this.value = values
        this.groupVisable = true
      }
    },
        
    // 5.2.6.更新组内设备列表
    async serialUpdate() {
      const { code } = await api.group(this).serialUpdate({
        hub_id: this.value
      }, this.id)
      if (code === 0) {
        this.$message.success('操作成功')
        this.closeGroup()
        this.getList()
      }
    },

    // 列表
    async getList() {
      const { code, data, total } = await api.group(this).getList({
        offset: this.offset,
        limit: this.limit,
      });
      if (code === 0) {
        this.tableData = data
        this.total = total
      }
    },


    async addOrEdit() {
      let fn = this.model.type
      const { code } = await api.group(this)[fn](this.form, this.id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.close();
        this.getList();
      }
    },

    async asyncDelete() {
      const { code } = await api.group(this).delete(this.id);
      if (code === 0) {
        this.$message.success("操作成功");
        this.id = "";
        this.visibility = false;
        if (this.total % this.limit === 1) {
          if (this.offset >= this.limit) {
            this.offset -= this.limit;
          }
        }
        this.getList();
      }
    },

  },
  created() {
    this.getList();
  },
};
</script>

<style lang="stylus">
.group {

}

</style>